// 重写数组文件
//1、获取
let oldArrayPrototMethods = Array.prototype;

//2、继承   Object.create() 静态方法以一个现有对象作为原型，创建一个新对象
export let ArrayMethods = Object.create(oldArrayPrototMethods);

//3、劫持
let methods = ['push', 'shift', 'unshift', 'pop', 'splice']
methods.forEach(method => {
  ArrayMethods[method] = function () {
    //调用原生方法
    let result = oldArrayPrototMethods[method].call(this, ...arguments)
    
    let inserted
    switch (method) {//push shift unshift pop splice
      case 'push':
      case 'shift':
      case 'unshift':
        inserted = arguments
        break;
      case 'splice':
        inserted = arguments.slice(2)
        break;
    }
    let ob = this.__ob__ //谁调用了就指向谁 this指向当前数组   this.__ob__得到observe实例
    if(inserted){
      ob.observeArray(inserted)  
    }
    return result
  }
})